import { warn } from '/@/utils/log';
let dynamicViewsModules: Record<string, () => Promise<Recordable>>;
import { EXCEPTION_COMPONENT } from '/@/router/constant';
export function dynamicImport(component: string) {
  // dynamicViewsModules = import.meta.glob('../../../../views/**/*.vue');
  dynamicViewsModules = import.meta.glob('../**/**/**/*.vue');
  // console.log(dynamicViewsModules);

  if (!component) return;
  const keys = Object.keys(dynamicViewsModules);
  const matchKeys = keys.filter((key) => {
    const k = key.replace('..', '');
    const startFlag = component.startsWith('/');
    const endFlag = component.endsWith('.vue');
    const startIndex = startFlag ? 0 : 1;
    const lastIndex = endFlag ? k.length : k.lastIndexOf('.');

    return k.substring(startIndex, lastIndex) === component;
  });
  if (matchKeys?.length === 1) {
    const matchKey = matchKeys[0];
    return dynamicViewsModules[matchKey];
  } else if (matchKeys?.length > 1) {
    warn(
      'Please do not create `.vue` and `.TSX` files with the same file name in the same hierarchical directory under the views folder. This will cause dynamic introduction failure',
    );
    return;
  } else {
    warn(
      '在src/components/ModelTable/src/components/customCom下找不到`' +
        component +
        '.vue` 或 `' +
        component +
        '.tsx`, 请自行创建!',
    );
    return EXCEPTION_COMPONENT;
  }
}
